package com.CmJava.controller;


import com.CmJava.entity.myinfo.Description;
import com.CmJava.entity.myinfo.MyInfo;
import com.CmJava.entity.ResultEntity;
import com.CmJava.entity.User;
import com.CmJava.service.AdminService;
import com.CmJava.utils.RsaUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@Controller
public class AdminController {

    @Autowired
    private AdminService adminService;

    @Autowired
    private RsaUtil rsaUtil;


    // 后台管理员登陆
    @RequestMapping("adminLogin")
    @ResponseBody
    public ResultEntity adminLogin(@RequestParam("adminAccount") String adminAccount,
                             @RequestParam("adminPassword") String adminPassword,
                             HttpSession session){

        // 设置超时时间，1小时
        session.setMaxInactiveInterval(3600);
        // 解密
        adminAccount= rsaUtil.decrypt(adminAccount);
        adminPassword= rsaUtil.decrypt(adminPassword);

        ResultEntity resultEntity = adminService.adminLogin(adminAccount, adminPassword);

        if (!resultEntity.getResultFlag()){
            return ResultEntity.failureWithoutMessage();
        }
        session.setAttribute("admin",(User)resultEntity.getData());

        return ResultEntity.successWithoutMessage();
    }

    // 后台管理员退出
    @RequestMapping("adminOut")
    @ResponseBody
    public ResultEntity adminOut(HttpSession session){
        if (session.getAttribute("admin")!=null) {
            session.removeAttribute("admin");
            return ResultEntity.successWithoutMessage();
        }else {
            return ResultEntity.failureWithoutMessage();
        }
    }


    // 个人信息
    @RequestMapping("myinfo")
    @ResponseBody
    public ResultEntity myinfo(HttpServletRequest request){


        ResultEntity resultEntity= adminService.getMyInfo(request);
        return resultEntity;
    }

    // 个人描述
    @RequestMapping("description")
    @ResponseBody
    public ResultEntity description(){
        ResultEntity resultEntity= adminService.getDescription();
        return resultEntity;
    }

    // 修改个人信息
    @RequestMapping("adminHtml/updateMyInFo")
    @ResponseBody
    public ResultEntity updateMyInFo(@RequestParam("degree") String degree,
                                     @RequestParam("email") String email,
                                     @RequestParam("address") String address,
                                     @RequestParam("telephone") String telephone){
        MyInfo myInfo = new MyInfo(1, "陈铭", degree, email, "1997.02.23", telephone, address);
        ResultEntity resultEntity= adminService.updateMyInFo(myInfo);
        return resultEntity;
    }

    // 修改个人描述
    @RequestMapping("adminHtml/updateDescription")
    @ResponseBody
    public ResultEntity updateDescription(@RequestParam("description") String description,
                                          @RequestParam("label1") String label1,
                                          @RequestParam("label1text") String label1text,
                                          @RequestParam("label2") String label2,
                                          @RequestParam("label2text") String label2text,
                                          @RequestParam("label3") String label3,
                                          @RequestParam("label3text") String label3text,
                                          @RequestParam("label4") String label4,
                                          @RequestParam("label4text") String label4text){
        Description description1 = new Description(1, description, label1, label1text, label2, label2text, label3, label3text, label4, label4text);
        ResultEntity resultEntity= adminService.updateDescription(description1);
        return resultEntity;
    }
}
